/**
 *
 */
package club.pinea.send;

import club.pinea.proto.BasicProto;
import club.pinea.session.IOSession;
import club.pinea.util.ProtoCommandMap;
import com.google.protobuf.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author admin
 */
public class MessageSend {
    static Logger log = LoggerFactory.getLogger(MessageSend.class);

//    public static void sendToUser(Message msg, ServerPlayer sp) {
//        if (sp == null) {
//            log.info("用户对象为空，不能发送消息：" + msg);
//            return;
//        }
//
//        sendToUser(msg, sp.getSession());
//    }

    /**
     * 失败的返回
     *
     * @return
     */
    public static BasicProto.Res errorRes() {
        return BasicProto.Res.newBuilder()
                .setStatus(BasicProto.ResStatus.ERROR_VALUE)
                .build();
    }

    /**
     * 失败的返回
     * @param ex
     * @return
     */
    public static BasicProto.Res errorRes(Exception ex) {
        return BasicProto.Res.newBuilder()
                .setStatus(BasicProto.ResStatus.ERROR_VALUE)
                .setDesc(ex.getMessage())
                .build();
    }

    /**
     * 失败的返回
     * @param message
     * @return
     */
    public static BasicProto.Res errorRes(String message) {
        return BasicProto.Res.newBuilder()
                .setStatus(BasicProto.ResStatus.ERROR_VALUE)
                .setDesc(message)
                .build();
    }

    /**
     * 成功的返回
     *
     * @return
     */
    public static BasicProto.Res successRes() {
        return BasicProto.Res.newBuilder()
                .setStatus(BasicProto.ResStatus.SUCCESS_VALUE)
                .build();
    }

    /**
     * 成功的返回
     *
     * @return
     */
    public static BasicProto.Res successRes(String message) {
        return BasicProto.Res.newBuilder()
                .setStatus(BasicProto.ResStatus.SUCCESS_VALUE)
                .setDesc(message)
                .build();
    }

    public static boolean sendToUser(Message msg, IOSession session) {
        if (session == null) {
            log.error("********sendToUser.session null");
            return false;
        }
        if (msg == null) {
            log.error("********sendToUser.msg null");
            return false;
        }
        try {
            Integer head = ProtoCommandMap.getCommand(msg.getDescriptorForType().getName());
            if (head != null) {
                BasicProto.BaseMessage.Builder im = BasicProto.BaseMessage.newBuilder();
                im.setOrder(head);
                im.setBody(msg.toByteString());
                session.send(im.build());
                log.info("发送消息【" + Integer.toHexString(head) + "】到客户端，大小：【" + im.build().getSerializedSize() + "】,消息体：" + msg.toString());
                return true;
            } else {
                log.error("发送客户端有误::" + msg.getDescriptorForType().getName());
            }
        } catch (Exception e) {
            log.error("发送用户消息失败。", e);
        }
        return false;
    }

    /**
     * 全服消息推送
     */
//    public static void sendToAllPlayers(Message msg) {
//        ConcurrentHashMap<Integer, ServerPlayer> allPlayers = GlobalServerPlayerContainer.getAllUser();
//        for (ServerPlayer sp : allPlayers.values()) {
//            sp.send(msg);
//        }
//    }

}
